<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../layui-v2.6.8/layui/css/layui.css">
    <script src="../layui-v2.6.8/layui/layui.js"></script>
    <script src="../js/laydate.js"></script>
    <script src="../js/jquery-2.1.0.min.js"></script>
    <script src="../js/layer.js"></script>
</head>
<body>

    <form class="layui-form" action="">
      <div class="layui-form-item">
        <label class="layui-form-label">书名</label>
        <div class="layui-input-inline">
          <input type="text" name="bookName" id="bookName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">数量</label>
        <div class="layui-input-inline">
          <input type="number" name="bookCounts" id="bookCounts" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">日期</label>
        <div class="layui-input-inline">
          <input type="text" id="date1" name="bookDate" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>
      </div>
        <!-- 输入书籍的详情信息 -->
        <div class="layui-form-item layui-form-text">
          <label class="layui-form-label">详情信息</label>
          <div class="layui-input-block">
            <textarea name="detail" id="detail" placeholder="请输入内容" class="layui-textarea"></textarea>
          </div>
        </div>
        <!-- 监听事件 -->
        <div class="layui-form-item">
          <div class="layui-input-block">
            <a class="layui-btn" lay-submit lay-filter="formDemo">立即提交</a>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
      <!--  -->
      
      <script>
      //Demo
      layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(data){
          $.ajax({
            type: 'POST', //请求类型
            url: 'http://localhost:8080/books/addBook',//请求操作 
            async: false, //异步操作
            data: {
                'bookName': $("#bookName").val(),
                'bookCounts': $("#bookCounts").val(),
                'bookDate': $("#date1").val(),
                'detail': $("#detail").val()
            },success: function (data){
                if(data.code!=200){
                    layer.msg(data.msg,{time: 3000, icon:5})
                }if(data.code==400){
                  layer.msg(data.msg,{time: 3000, icon:5})
                }else{
                  layer.msg(data.msg,{time: 1000, icon:1 ,end:function () {
                            // location.reload();
                            //添加成功之后等待两秒之后跳转到 表格界面
                            window.location="http://127.0.0.1:5500/test_01/template.html";//当前页面打开URL页面
                        }})
                         //重定向
                }
            }
        });
        });
      });
      //加载laidate模块
      layui.use('laydate',function(){
        var laydate=layui.laydate;
        laydate.render({
          elem:'#date1',
          // 默认时间选择器 
        });
      });
      </script>       
</body>
</html>